package com.web.exiaodao.remote.service.impl;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.entity.ConfigurationTable;
import com.web.exiaodao.business.repository.ConfigurationTableRepository;
import com.web.exiaodao.remote.service.ApiConfigurationTableService;
import com.web.exiaodao.remote.vo.ConfigurationTableVo;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: zhangl
 * @times: 2015-5-15下午06:46:35
 * 类的说明：前端APIConfigurationTable接口实现类
 **/
@Service("ApiConfigurationTableService")
@Transactional(readOnly = false)
public class ApiConfigurationTableServiceImpl extends BaseService<ConfigurationTable> implements ApiConfigurationTableService {

    private static final Logger LOGGER = Logger.getLogger(ApiConfigurationTableServiceImpl.class);

    @Autowired
    ConfigurationTableRepository configurationTableRepository;

    @Override
    public XaResult<ConfigurationTableVo> findLastOne() throws BusinessException {
        XaResult<ConfigurationTableVo> xr = new XaResult<>();
        Map<String, SearchFilter> filters = new HashMap<>();
        filters.put("status", new SearchFilter("status", Operator.NE, XaConstant.Status.delete));
        List<ConfigurationTable> entitys = configurationTableRepository.findAll(DynamicSpecifications
                .bySearchFilter(filters.values(), ConfigurationTable.class), new Sort(
                new Sort.Order[]{
                        new Sort.Order(Sort.Direction.DESC, "createTime"),
                        new Sort.Order(Sort.Direction.DESC, "id")
                }));
        if (entitys == null || entitys.size() == 0) {
            xr.error(XaConstant.Message.object_not_config);
        } else {
            xr.setObject(JSON.parseObject(JSON.toJSONString(entitys.get(0)), ConfigurationTableVo.class));
            xr.getObject().setId(entitys.get(0).getId());
            LOGGER.info("查询单个实体----->ConfigurationTable");
        }
        return xr;
    }


    /**
     * 查询今日签到可以获取的亲币 签到规则  签到计算
     * 亲币规则(首日签到100,第二天140,第三条以后190;如果中断从100开始)
     * @param checkCountDays 连续签到的天数，>= 1
     * @return
     * @throws BusinessException
     */
    @Override
    public XaResult<Integer> signMoneyByCheckCount(int checkCountDays) throws BusinessException {
        XaResult<Integer> xr = new XaResult<>();
        if (checkCountDays < 0) {
            xr.error("输入参数异常(checkCountDays)");
            return xr;
        }
        XaResult<ConfigurationTableVo> cfXr =  findLastOne();
        if (cfXr.getCode() != XaConstant.Code.success) {
            xr.error(cfXr.getMessage());
            throw new BusinessException(cfXr.getMessage());
        } else {
            //首签获亲币/天
            Double  signInCoinFirst =  cfXr.getObject().getIncome_expenses_signin_coin();//第一天签到获取的金币数(默认100)
            Integer ascCoin         =  cfXr.getObject().getIncome_expenses_signin_asc_coin();
            if (signInCoinFirst == null || ascCoin == null) {
                xr.error("income_expenses_signin_coin或者income_expenses_signin_asc_coin没有配置，请先进行配置！");
                return xr;
            }
            //10
            if (checkCountDays == 1) {
                xr.setObject(signInCoinFirst.intValue());
                //15;20;25;30
            }  else {//2,3,4,5
                //其余是等差递增
                checkCountDays = checkCountDays >= 5 ? 5 : checkCountDays;
                int coins = (checkCountDays - 1) * ascCoin.intValue()  + signInCoinFirst.intValue();
                xr.setObject(coins);
            }
        }
        return xr;
    }


    @Override
    public XaResult<ConfigurationTableVo> findOne(Long tId)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<List<ConfigurationTableVo>> findListNEStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<List<ConfigurationTableVo>> findListEQStatusByFilter(
            Integer status, Map<String, Object> filterParams, Pageable pageable)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<ConfigurationTableVo> multiOperate(String modelIds,
                                                       Integer status) throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<ConfigurationTableVo> createModel(ConfigurationTable model)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

    @Override
    public XaResult<ConfigurationTableVo> updateModel(ConfigurationTable model)
            throws BusinessException {
        throw new BusinessException(XaConstant.Message.error_method_no_impl);
    }

}
